package tmproject.hlhj.fhp.cartrans.modules

import android.content.Context
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import com.example.mymvp.BaseModule
import com.lzy.okgo.model.Response
import io.reactivex.disposables.Disposable
import tmproject.hlhj.fhp.cartrans.activitys.RegisAty
import tmproject.hlhj.fhp.cartrans.convert.MyException
import tmproject.hlhj.fhp.cartrans.http.Apis
import tmproject.hlhj.fhp.cartrans.http.BaseAciton
import tmproject.hlhj.fhp.cartrans.http.LoadingAction
import tmproject.hlhj.fhp.cartrans.javaBean.BaseBean
import tmproject.hlhj.fhp.cartrans.javaBean.LoginBean
import tmproject.hlhj.fhp.cartrans.utils.ToastUtils

/**
 * Created by Administrator on 2018/8/11/011.
 */
class LoginModule :BaseModule {
    var listener:LoginResp?=null
    fun goLogin(c:Context,mobile:String,sms_code:String):String{
        Apis.login(mobile, sms_code)
                ?.let {
                    it.doOnSubscribe(LoadingAction(c))
                            .subscribe(object :BaseAciton<Response<LoginBean>>{
                                override fun onSubscribe(d: Disposable) {

                                }

                                override fun httpSuccess(t: Response<LoginBean>) {
                                    listener?.loginResult(t.body())
                                }
                                override fun httpErroCode(baseBean: BaseBean) {

                                }
                                override fun httpFailed(e: Throwable?) {
                                }

                                override fun onError(e: Throwable) {
//                                    super.onError(e)
                                    if (e is MyException){
                                        if (e.code==408){
                                            ToastUtils.toast("请填写注册信息")
                                            if (c is AppCompatActivity){
                                                val intent = Intent(c, RegisAty::class.java)

                                                c.startActivity(intent)
                                            }
                                        }

                                    }else{
                                        ToastUtils.toast(e?.message!!)
                                    }
                                }
                            })
                }
        return ""
    }

    fun sendSms(context: Context,phone:String){
        Apis.sendSms(phone)
                ?.let {
                    it.doOnSubscribe(LoadingAction(context))
                            .subscribe(object :BaseAciton<Response<BaseBean>>{
                                override fun onSubscribe(d: Disposable) {

                                }

                                override fun httpSuccess(t: Response<BaseBean>) {
                                    listener?.smsResp(t.body())
                                }
                                override fun httpErroCode(baseBean: BaseBean) {
                                }

                                override fun httpFailed(e: Throwable?) {
                                }
                            })
                }
    }

    interface LoginResp{
        fun loginResult(loginBean: LoginBean)
        fun smsResp(baseBean: BaseBean)
    }
}